import server from '@/config/server/index.js'
import ruleOpt from './rule.js'

const api = {
  getUserByNameOrPhone: server.api().get('/hft-portal/getUserByNameOrPhone'),
  getCustomerByPhoneOrName: server.api().post('/hft-portal/v1/customer/getCustomerByPhoneOrName'), // 客户姓名
  getBrokerId: (type) => {
    // 1 门店负责人 2 机构负责人
    return type == 1
      ? server.api().get('/hft-portal/getBrokerId')
      : type == 2
      ? server.api().post('/hft-portal/getInsBrokerId')
      : null
  }
}

// 姓名 手机号查询责任人
function getNameOrPhone(nameOrPhone, params) {
  let data = {
    nameOrPhone,
    ...params
  }
  return api.getUserByNameOrPhone.fetch(data).then((res) => {
    return (
      res &&
      res.map((item) => {
        item.key = item.userId
        item.value = item.userName + '-' + item.phone
        return item
      })
    )
  })
}

function createNameOrPhone(opt = {}, params) {
  // key title
  return {
    title: opt.title,
    key: opt.key,
    rules: opt.rules,
    placeholder: '请输入' + opt.title,
    disabled: opt.disabled,
    select: {
      filterable: true,
      options: opt.options || [],
      remote: (key) => {
        return getNameOrPhone(key, params)
      }
    },
    data: {
      from: (info) => {
        return info[opt.key]
      },
      to: (v, o) => {
        let info = {
          userId: null,
          userName: ''
        }
        if (v) {
          info.userId = v.value
          info.userName = v.label
        }
        if (opt.to) {
          return opt.to(Object.assign(o, info))
        }
        return info
      }
    }
  }
}

//查询门店人员用 根据用户名称或手机号获取用户数据
function getBrokerId(userNameOrPhone, params, type) {
  return api
    .getBrokerId(type)
    .fetch({
      userNameOrPhone,
      ...params
    })
    .then(
      (res) =>
        res &&
        res.map((item) => {
          item.key = item.userId
          item.value = item.userName + '-' + item.phone
          return item
        })
    )
}

function createBroker(opt = {}, params, type = 1) {
  // key title
  return {
    title: opt.title,
    key: opt.key,
    placeholder: '请输入' + opt.title,
    select: {
      filterable: true,
      options: opt.options || [],
      remote: (key) => {
        return getBrokerId(key, params, type)
      }
    },
    change: opt.change,
    data: {
      from: (info) => {},
      to: (v, o) => {
        let info = {
          userId: null,
          userName: ''
        }
        if (v) {
          info.userId = v.value
          info.reporterId = v.value
          info.userName = v.label
        }
        if (opt.to) {
          return opt.to(Object.assign(o, info))
        }
        return info
      }
    }
  }
}

// 客户姓名
function getCustomerNameOrPhone(nameOrPhone, params) {
  let data = {
    nameOrPhone,
    ...params
  }
  return api.getCustomerByPhoneOrName.fetch(data).then((res) => {
    return (
      res &&
      res.map((item) => {
        item.key = item.customerId
        item.value = item.name + '-' + item.phone
        return item
      })
    )
  })
}

function createCustomer(opt = {}, params) {
  // key title
  return {
    title: opt.title,
    key: opt.key,
    placeholder: '请输入' + opt.title,
    select: {
      filterable: true,
      options: opt.options || [],
      remote: (key) => {
        return getCustomerNameOrPhone(key, params)
      }
    },
    change: opt.change,
    data: {
      from: (info) => {},
      to: (v, o) => {
        let info = {
          customerId: null
        }
        if (v) {
          info.customerId = v.value
        }
        if (opt.to) {
          return opt.to(Object.assign(o, info))
        }
        return info
      }
    }
  }
}

export default {
  getNameOrPhone,
  getCustomerNameOrPhone,
  createNameOrPhone,
  createBroker,
  createCustomer
}
